package com.qiyu.admin.system.controller;

import com.qiyu.admin.system.model.SysFile;
import com.qiyu.admin.system.model.User;
import com.qiyu.admin.system.service.SysFileService;
import com.qiyu.admin.system.service.UserService;
import com.qiyu.admin.system.shiro.EncryptPwd;
import com.qiyu.admin.system.util.SystemConstant;
import com.qiyu.admin.system.util.ModelUtil;
import com.qiyu.framework.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

import static com.qiyu.framework.util.QiYuUtil.getResponseJson;


/**
 * 用户管理
 *
 * @author zhangqing
 * @date 2017年02月28日
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private SysFileService sysFileService;

    @Value(value = "${qiyu.default.photo.path}")
    private String defaultPath;


    /**
     * 用户管理主页面
     * @return
     */
    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public ModelAndView userMain(){
        return new ModelAndView("system/user/user_list");
    }
    /**
     * 分页查询用户集合
     * @return UserSample
     */
    @RequestMapping(value="/page/list",method = RequestMethod.GET)
    @ResponseBody
    public PagedResult<User> getUserPageList(PageBean pageBean, User user)throws Exception{
        return userService.selectPageList(pageBean.getPageNumber(),pageBean.getPageSize(),user);
    }
    /**
     * 用户profile页面
     * @return
     */
    @RequestMapping(value = "/profile/view/{userId}",method = RequestMethod.GET)
    public ModelAndView userUpdateView(@PathVariable(value = "userId") String userId, String operator, ModelMap modelMap) throws Exception{
        User userInfo = userService.selectByPrimaryKey(userId);
        if(HelpUtils.isNotEmpty(operator) && HelpUtils.isNotEmpty(userInfo)){
            SysFile sysFile = new SysFile();
            sysFile.setParentId(userId);//根据用户主键查询头像
            List<SysFile> sysFileList = sysFileService.select(sysFile);
            if(HelpUtils.isNotEmpty(sysFileList)){//不为空,说明是修改操作
                modelMap.put("filePath","http://121.42.166.32/"+sysFileList.get(0).getFilePath());
            }else{
                modelMap.put("filePath",defaultPath);
            }
            modelMap.put("operator",operator);
            modelMap.put("userInfo",userInfo);
            return new ModelAndView("system/user/user_profile",modelMap);
        }else{
            modelMap.put("status",QiYuConstant.ERROR_CODE_500);
            modelMap.put("error","请填写准确的参数!");
            return new ModelAndView(QiYuConstant.ERROR_PAGE,modelMap);
        }
    }
    @RequestMapping(value = "/add/view",method = RequestMethod.GET)
    public  ModelAndView userAddView(){
        return new ModelAndView("system/user/user_add");
    }

    /**
     * 增加用户操作
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseJson userAdd(User user) throws Exception{
        ModelUtil.insertInit(user);
        new EncryptPwd().encryptPassword(user);
        userService.insertSelective(user);
        return getResponseJson(SystemConstant.ADD_SUCCESS,true,null);
    }
    /**
     * 修改用户操作
     * @param user
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResponseJson userUpdate(User user) throws Exception{
        ModelUtil.updateInit(user);
        if(HelpUtils.isNotEmpty(user.getLoginPwd())){//修改了密码
            if(user.getLoginName().equalsIgnoreCase("admin")){
                return getResponseJson("请不要修改管理员的密码",false,null);
            }
            new EncryptPwd().encryptPassword(user);
        }
        userService.updateByPrimaryKeySelective(user);
        return getResponseJson(SystemConstant.UPDATE_SUCCESS,true,null);
    }


    /**
     * 验证登录账号是否唯一
     * @param loginName
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uniqueness/loginName")
    public Object checkLoginName(String loginName) {
        try {
            return QiYuUtil.getValidJson(userService.isExitLoginName(loginName));
        } catch (Exception e) {
            return QiYuUtil.getValidJson(false);
        }
    }
    /**
     * 批量删除用户
     * @param idList 用户集合
     *
     */
    @RequestMapping(value = "/batch",method = RequestMethod.DELETE)
    public ResponseJson deleteUsers(@RequestBody List<String> idList) throws Exception{
        boolean isSuccess = userService.deleteByPrimaryKeyList(idList);
        return getResponseJson("删除成功",isSuccess,null);
    }
}
